package com.livsdesign.tools

fun Byte.toUInt8(): Int {
    return this.toInt().and(0xff)
}

fun toUInt16(byte1: Byte, byte2: Byte): Int {
    return (byte1.toUInt8() shl 8).or(byte2.toUInt8())
}

fun ByteArray.toHexString(separator: Char? = null): String {
    if (isEmpty()) return "Empty Byte Array"
    val sb = StringBuilder()
    for (index in 0 until lastIndex) {
        val tmp = this[index].toUInt8()
        sb.append(String.format("%02X", tmp))
        if (separator != null) {
            sb.append(separator)
        }
    }
    sb.append(String.format("%02X", last().toUInt8()))
    return sb.toString()
}